package com.pengtu.web.asset.assets;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.asset.office.Information;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.asset.AssetsManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.ExcelHelper;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

//覆盖默认的nameSpace
@Namespace("/asset/assets")
// 定义名为reload的result重定向到CarInfomation.action, 其他result则按照convention默认.
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "house!list.action", type = "redirect") })
public class HouseAction extends BaseActionSupport<Information> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AssetsManager assetsManager;
	private AccountManager accountManager;

	private String id;
	private Information asset;
	private List<Information> assetInfoList;
	private Page<Information> page = new Page<Information>(10);
	private Department dept;
	private Attachment attachment;

	public void setId(String id) {
		this.id = id;
	}

	public List<Information> getAssetInfoList() {
		return assetInfoList;
	}

	public Page<Information> getPage() {
		return page;
	}

	/**
	 * 检查编号是否重复
	 * 
	 * @return
	 */
	public void findByUniqueCode() {

		String newCode = Struts2Utils.getParameter("code");
		String oldCode = Struts2Utils.getParameter("oldCode");

		if (assetsManager.isUniqueCode(newCode, oldCode)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}

	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		assetsManager.deleteInformation(id);
		addActionMessage("删除资产信息!");
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id))
			dept = asset.getDepartment();
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		queryFilter.addFilter("type","2");
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = assetsManager.findPageAsset(page, queryFilter);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {

		if (StringUtils.isNotEmpty(id)) {
			asset = assetsManager.getInformation(id);
		} else {
			asset = new Information();
		}
	}

	@Override
	public String save() throws Exception {
		String deptId = Struts2Utils.getParameter("deptId");
		if (StringUtils.isNotEmpty(deptId)) {
			dept = accountManager.getDepartment(deptId);
			asset.setDepartment(dept);
		}
//		try {
//			String oldPrice  = Struts2Utils.getParameter("newPrice");
//			oldPrice = oldPrice.substring(1);
//			Double newPrice = new Double(oldPrice);
//			asset.setPrice(newPrice);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			asset.setPrice(0d);
//		}
		assetsManager.saveInformation(asset);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"Id为" + asset.getId() + "的资产信息");
		return RELOAD;
	}
	
	/**
	 * 从Excel中读取数据保存
	 * importassets:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String importassets() {
		
		if (null != attachment) {
			try {
				File file = attachment.getUpfile();
				Workbook book = Workbook.getWorkbook(file);
				// 获得第一个工作表对象
				Sheet sheet = book.getSheet(0);
				int rownum = sheet.getRows(); // 得到行数
				// 得到第一列第一行的单元格
				ExcelHelper excelHelper = ExcelHelper.getInstance();
				for (int i = 3; i < rownum; i++) // 循环进行读写
				{
					if(StringUtils.isEmpty(excelHelper.getValueAt(sheet, i, 0)))
						continue;
					Information asset = new Information();
					asset.setName(excelHelper.getValueAt(sheet, i, 0));
					// 保存部门
					String depName = excelHelper.getValueAt(sheet, i, 1);
					Department dept = accountManager.findByDepartment("depName",depName);
					if(null!=dept)
						asset.setDepartment(dept);
					asset.setCode(excelHelper.getValueAt(sheet, i,2));
					asset.setType(excelHelper.getValueAt(sheet, i, 3));
					asset.setModel(excelHelper.getValueAt(sheet, i, 4));
					// 保存资产价格
					try {
						Double price = Double.valueOf(excelHelper.getValueAt(sheet, i, 5));
						asset.setPrice(new Double(price));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						asset.setPrice(0.0);
					}
					asset.setBrand(excelHelper.getValueAt(sheet, i, 6));
					asset.setBuyer(excelHelper.getValueAt(sheet, i, 7));
					// 购置时间
					SimpleDateFormat buyDate = new SimpleDateFormat(DateUtils.getDefaultDatePattern());
					if (StringUtils.isNotEmpty(excelHelper.getValueAt(sheet, i,
							8)))
						asset.setBuydate(buyDate.parse(excelHelper.getValueAt(
								sheet, i, 8)));

					asset.setStored(excelHelper.getValueAt(sheet, i, 9));
					String recorder = SpringSecurityUtils.getCurrentUserName();
					asset.setRecorder(recorder);
					asset.setRecorddate(new Date());
					asset.setRemark(excelHelper.getValueAt(sheet, i, 10));
					assetsManager.saveInformation(asset);
				}
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			return "successMessage";
		}
		return null;
	}

	@Override
	public Information getModel() {
		return asset;
	}

	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public Attachment getAttachment() {
		return attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	@Autowired
	public void setAssetsManager(AssetsManager assetsManager) {
		this.assetsManager = assetsManager;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

}
